package cn.tedu.boyou.acticle.service.impl;

import cn.tedu.boyou.acticle.domain.Destination;
import cn.tedu.boyou.acticle.domain.Region;
import cn.tedu.boyou.acticle.mapper.DestinationMapper;
import cn.tedu.boyou.acticle.mapper.RegionMapper;
import cn.tedu.boyou.acticle.service.IDestinationService;
import cn.tedu.boyou.common.query.DestinationQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Transactional  //单体事务控制
public class DestinationServiceImpl implements IDestinationService{
    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private RegionMapper regionMapper;

    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> destinations = destinationMapper.selectForList(qo);
        return new PageInfo(destinations);
    }

    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectDestByDeep(deep);
    }

    public List<Destination> queryDestByRegionId(Long rid) {
        //需要执行一个分支：国内分支
        if(rid == -1){
            return destinationMapper.selectDestByParentId(1L);
        }

        Region region = regionMapper.selectByPrimaryKey(rid);

        //select from destination where ??
        //某个区域下关联的目的地id
        Long[] refIds = region.getRefIds();


        return destinationMapper.selectDestByIds(refIds);
    }

    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        //根>中国>广东>广州
        //parentId = 广州id
        //广州
        //广州.getParentId() 广东
        //广东.getParentId() 中国
        //根
        createToasts(parentId,list);
        Collections.reverse(list);
        return list;
    }

    public void changeHotValue(Long id, Boolean hot) {
        destinationMapper.changeHotValue(id,hot);
    }

    public void setInfo(Long id, String info) {
        destinationMapper.updateInfo(id,info);
    }

    public Destination get(Long destId) {
        return destinationMapper.selectByPrimaryKey(destId);
    }

    public List<Destination> listAll() {
        return destinationMapper.selectAll();
    }

    public Destination getCountry(Long id) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if(destination.getParent() == null){
            return destination;
        }
        List<Destination> toasts = this.getToasts(id);
        if(toasts != null && toasts.size() >0){
            return toasts.get(0);
        }
        return null;
    }

    private Boolean toastCheck(Long id) {
        Boolean flag = null;
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if ("中国".equals(destination.getName())) {
            return false;
        }
        if (destination.getParent()==null) {
            return true;
        }
        flag = toastCheck(destination.getParent().getId());
        return flag;
    }


    public Destination getProvince(Long id) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if (destination==null) {
            return null;
        }
        if (destination.getParent()==null) {
            return null;
        }
        List<Destination> toasts = new ArrayList<>();
        icreateToast(id,toasts);
        Collections.reverse(toasts);
        return toasts.get(1);
    }

    public List<Destination> getDestNameByDeep(int deep) {
        return destinationMapper.selectDestNameBydeep(deep);
    }

    public Destination getParentByName(Long destId) {
        Destination hasParent = destinationMapper.selectParentByName(destId);
        return hasParent;
    }

    private void createToasts(Long parentId,List<Destination> list){
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if(destination == null){
            return ;
        }
        list.add(destination);

        if(destination.getParent() != null){
            createToasts(destination.getParent().getId(),list);
        }
    }

    private void icreateToast(Long id, List<Destination> toasts) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        toasts.add(destination);
        if (destination.getParent()==null) {
            return;
        }
        icreateToast(destination.getParent().getId(),toasts);
    }


}
